# -*- coding: utf-8 -*-
from Preset.Model.PartBase import PartBase
from Preset.Model.GameObject import registerGenericClass


@registerGenericClass("TutorialPart")
class TutorialPart(PartBase):
	def __init__(self):
		super(TutorialPart, self).__init__()
		self.name = "监听生成物品零件"

	def InitClient(self):
		pass

	def InitServer(self):
		import mod.server.extraServerApi as serverApi
		serverSystem = serverApi.GetSystem("Minecraft", "preset")
		serverSystem.ListenForEvent(serverApi.GetEngineNamespace(), serverApi.GetEngineSystemName(), "ServerChatEvent", self, self.OnServerChat)

	def OnServerChat(self, args):
		# 生成掉落物品
		# 当我们输入的信息等于右边这个值时，创建相应的物品
		# 创建Component，用来完成特定的功能，这里是为了创建Item物品
		import mod.server.extraServerApi as serverApi
		playerId = args["playerId"]
		comp = serverApi.CreateComponent(playerId, "Minecraft", "item")
		if args["message"] == "钻石剑":
			# 调用SpawnItemToPlayerInv接口生成物品到玩家背包，参数参考《MODSDK文档》
			comp.SpawnItemToPlayerInv({"itemName":"minecraft:diamond_sword", "count":1, 'auxValue': 0}, playerId)
		elif args["message"] == "钻石镐":
			comp.SpawnItemToPlayerInv({"itemName":"minecraft:diamond_pickaxe", "count":1, 'auxValue': 0}, playerId)
		elif args["message"] == "钻石头盔":
			comp.SpawnItemToPlayerInv({"itemName":"minecraft:diamond_helmet", "count":1, 'auxValue': 0}, playerId)
		elif args["message"] == "钻石胸甲":
			comp.SpawnItemToPlayerInv({"itemName":"minecraft:diamond_chestplate", "count":1, 'auxValue': 0}, playerId)
		elif args["message"] == "钻石护腿":
			comp.SpawnItemToPlayerInv({"itemName":"minecraft:diamond_leggings", "count":1, 'auxValue': 0}, playerId)
		elif args["message"] == "钻石靴子":
			comp.SpawnItemToPlayerInv({"itemName":"minecraft:diamond_boots", "count":1, 'auxValue': 0}, playerId)

	def TickClient(self):
		pass

	def TickServer(self):
		pass

	def DestroyClient(self):
		pass

	def DestroyServer(self):	
		import mod.server.extraServerApi as serverApi
		serverSystem = serverApi.GetSystem("Minecraft", "preset")
		serverSystem.UnListenForEvent(serverApi.GetEngineNamespace(), serverApi.GetEngineSystemName(), "ServerChatEvent", self, self.OnServerChat)
